/*
 * Copyright (c) 2023 IUV Inc. All rights reserved.
 * 项目名称：IUV TXY
 * 文件名：RegistController.java
 * Date：23-10-10 下午2:09
 * Author：Will
 */

package com.iuv.baike.controller;

import com.iuv.baike.common.CommonOperateCode;
import com.iuv.baike.dto.OnlineHourInfo;
import com.iuv.baike.service.DemoBaikeOnlineHourService;
import com.iuv.baike.util.RespondUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * BaiKeOnlineHourController
 * 控制器
 *
 * @author Will
 * @date 16:33
 */

@Controller
@RequestMapping("/onlineHour")
public class BaiKeOnlineHourController {
    private static Logger logger = LoggerFactory.getLogger(BaiKeOnlineHourController.class);
    
    @Autowired
    private DemoBaikeOnlineHourService demoBaikeOnlineHourService;

    /**
     * 根据号码查询在网时长
     *
     * @param mobile
     * @return
     */
    @RequestMapping("queryOnlineHour")
    @ResponseBody
    public Map<String, Object> queryOnlineHour(@RequestParam String mobile,String carrier) {

        logger.info("查询在网时长，手机号码为：" + mobile);
        mobile = StringUtils.trimToNull(mobile);
        OnlineHourInfo onlineHourInfo = null;
        Map<String, Object> repond = null;

        // 1. 如果手机号为空，返回MOBILE_NOT_NULL业务错误信息
        if (mobile == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.MOBILE_NOT_NULL, CommonOperateCode.SysMsg.MOBILE_NOT_NULL);
        }

        // 2. 先查询本地数据库
        onlineHourInfo = demoBaikeOnlineHourService.queryByMobile(mobile);

        // 3. 未查询到状态信息，再通过云端进行查询
        if (onlineHourInfo == null) {
            String realCarrierCode = realCarrierCode(carrier);
            onlineHourInfo = demoBaikeOnlineHourService.queryOverYun(mobile, realCarrierCode);
        }

        // 4. 都查询不到，则返回QUERY_ERROR业务错误信息
        if (onlineHourInfo == null) {
            return RespondUtil.respondFail(CommonOperateCode.SysCode.QUERY_ERROR, CommonOperateCode.SysMsg.QUERY_ERROR);
        }

        // 5. 查询到，则返回操作成功信息，并携带手机在线信息
        repond = RespondUtil.respondSuccess(null, "查询成功");
        repond.put("data", onlineHourInfo);
        return repond;
    }

    private String realCarrierCode(String carrierCode)
    {
        String realCarrierCode=null;
        if(carrierCode!=null)
        {
            if("中国移动".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="CMCC";
            }else if("中国联通".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="CUCC";
            }else if("中国电信".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="CTCC";
            }else if("中国广电".equalsIgnoreCase(carrierCode))
            {
                realCarrierCode="CBN";
            }
        }
        return realCarrierCode;
    }
}